<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class JifenDetail extends Base
{
    //获取积分兑换记录
    public function getPageList($options=[],$where=[],$whereDates,$lang='zn'){
        try {
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->leftJoin('users','users.id','=','jifen_details.uid');
            if(!empty($whereDates)){
                $model = $model->whereDate('jifen_details.created_at','>=',$whereDates[0])->whereDate('jifen_details.created_at','<',$whereDates[1]);
            }
            $model=$model->select('jifen_details.*','users.username','users.email','users.contact','users.total','users.username_zn')
                ->where($where)->orderBy('jifen_details.created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }


    //形成交易单
    public function transAdd($data,$user,$lang="zn"){
        try {
            $model = $this;
            //形成交易单数据
            $randstr = Str::random(4);
            $transNo = date('YmdHis').$randstr;
            //获取订单信息
            switch ($data['transtype']){
                case 1: //积分兑换
                    $order = (new ProdOrder())->where([
                        ['id','=',$data['orderid']],['ordtype','=',2],['uid','=',$user->id]
                    ])->first();
                    if(!$order){
                        return [
                            'code'=>1,
                            'message'=>$this->lang['ORDER_UNKOWN'][$lang]
                        ];
                    }
                    if($data['paytype']==1){ //钱包支付
                        if($user->balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $model = new Transition();
                        $model->transname = '积分商品付款';
                        $model ->transtype = 3;
                        $model->balance = $user->balance - $data['number'];
                        $model->cycle_balance = $user->cycle_balance;
                        $model->paytype = 1;
                    }elseif ($data['paytype']==2){ //反馈钱包支付
                        if($user->cycle_balance < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['CYCLE_BALANCE_NOENOUGH'][$lang]
                            ];
                        }
                        $model = new Transition();
                        $model->transname = '积分商品反馈钱包付款';
                        $model ->transtype = 3;
                        $model->balance = $user->balance;
                        $model->cycle_balance = $user->cycle_balance - $data['number'];
                        $model->paytype = 2;
                    }else{ //积分支付
                        if($user->coin < $data['number']){
                            return [
                                'code'=>1,
                                'message'=>$this->lang['JIFEN_NOENOUGH'][$lang]
                            ];
                        }
                        $model->coin = $user->coin - $data['number'];
                        $model->transname = '积分商品兑换';
                        $model ->transtype = $data['transtype'];
                    }
                    break;
                case 2: //反馈积分
                    $model ->transname = '用户积分反馈';
                    $model->coin = $user->coin + $data['number'];
                    $model ->transtype = $data['transtype'];
                    break;
            }
            $model ->uid = $user->id;
            $model ->number = $data['number'];
            $model ->transno = $transNo;
            $model ->orderid = $data['orderid'];
            $model ->status = 1;
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$transNo
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>$this->lang['TRANS_CRE_FAIL'][$lang]
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //积分支付
    public function transComplete($data,$user,$lang='zn'){
        try {
            $transition = $this->where('transno',$data['transno'])->first();
            //比较交易数据是否一致
            if($data['number']!=$transition->number){
                return ['code'=>1,'message'=>$this->lang['TRANS_NUM_ERR'][$lang]];
            }
            $now = date('Y-m-d H:i:s');
            $order = (new ProdOrder())->where('id',$transition->orderid)->first();
            $order->status = 2;
            $order->paytime = $now;
            $transition->status = 2;
            $transition->coin = $user->coin - $data['number'];
            $user->coin -= $data['number'];
            DB::transaction(function ()use ($transition,$user,$order){
                $transition->save();
                $user->save();
                $order->save();
            },5);
            return [
                'code'=>0,
                'data'=>[
                    'transno'=>$data['transno'],
                    'number'=>$data['number']
                ]
            ];
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()// $this->lang['ERROR'][$lang]
            ];
        }
    }
}
